package com.cps116.friendstracker;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.easy.facebook.android.apicall.FQL;
import com.easy.facebook.android.error.EasyFacebookError;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class QueryActivity_FriendsFrequentWhereIFrequentActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.queryactivity_friendsfrequentwhereifrequent_layout);

		FQL facebookFQL = ((FriendsTrackerApplication) getApplication()).getFacebookFQL();
		ArrayList<String> resultArray = new ArrayList<String>();
		
		//this works, but it takes a decent amount of time to run because of the multiple FQL queries...
		try {
			String resultXML = facebookFQL.fqlQuery("SELECT page_id, author_uid " +
													"FROM checkin " +
													"WHERE page_id IN (SELECT page_id " +
																	  "FROM checkin " +
									  								  "WHERE author_uid = me()) " +
									  				"AND author_uid != me()");

			Pattern p = Pattern.compile("<page_id>(.+?)</page_id>(\\s+)<author_uid>(.+?)</author_uid>");
			Matcher m = p.matcher(resultXML);
			
			Pattern namePattern = Pattern.compile("<name>(.+?)</name>");
			
			while(m.find())
			{
				
				String userNameXML = facebookFQL.fqlQuery("SELECT name " +
													   "FROM user " +
													   "WHERE uid = " + m.group(3));
				
				String placeNameXML = facebookFQL.fqlQuery("SELECT name " +
												 		"FROM place " +
												 		"WHERE page_id = " + m.group(1));
				
				Matcher nameMatcher = namePattern.matcher(userNameXML);
				String userName = "";
				if(nameMatcher.find()) userName = nameMatcher.group(1);
				
				nameMatcher = namePattern.matcher(placeNameXML);			
				String placeName = "";
				if(nameMatcher.find()) placeName = nameMatcher.group(1);
				
				if(! resultArray.contains(userName + " @ " + placeName)) resultArray.add(userName + " @ " + placeName);	
			}
		} 
		catch (EasyFacebookError e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] result;
		
		if(resultArray.isEmpty())
		{
			result = new String[1];
			result[0] = "No Friends who have Checked into your Check-ins";
		}
		else
		{
			result = new String[resultArray.size()];

			for(int i = 0; i < resultArray.size(); i++)
			{
				result[i] = resultArray.get(i);
			}
		}
		
		ListView list=(ListView)findViewById(R.id.queryactivity_friendsfrequentwhereifrequent_listview);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, result);
		list.setAdapter(adapter);
		
		
		/* this works, it just takes so long that it becomes impractical to use
		
		try 
		{
			//have to use multiple FQL queries because FQL doesn't support crossing of tables
			String myCheckinsXML = facebookFQL.fqlQuery("SELECT checkin_id " +
													 "FROM checkin " +
													 "WHERE author_id = me()");
			
			Pattern checkinPattern = Pattern.compile("<checkin_id>(.+?)</checkin_id>");
			Matcher checkinMatcher = checkinPattern.matcher(myCheckinsXML);
			
			ArrayList<String> results = new ArrayList<String>();
			ArrayList<String> myCheckins = new ArrayList<String>();
			
			while(checkinMatcher.find())
			{
				myCheckins.add(checkinMatcher.group(1));
			}
			
			Pattern friendsPattern = Pattern.compile("(.+?),(.+?)");

			for(int i = 0; i < friends.size(); i++)
			{
				Matcher friendsMatcher = friendsPattern.matcher(friends.get(i));
				if(friendsMatcher.find())
				{					
					String theirCheckinsXML = facebookFQL.fqlQuery("SELECT checkin_id " +
																"FROM checkin " +
																"WHERE author_id = " + friendsMatcher.group(2) + ")");
					
					checkinMatcher = checkinPattern.matcher(theirCheckinsXML);
					
					while(checkinMatcher.find())
					{
						if(myCheckins.contains(checkinMatcher.group(1)))
						{
							String placeNameXML = facebookFQL.fqlQuery("SELECT name " +
																	   "FROM place " +
																	   "WHERE page_id IN (SELECT page_id " +
																	   					 "FROM checkin " +
																	   					 "WHERE checkin_id = " + checkinMatcher.group(1) + ")");
							Pattern p = Pattern.compile("<name>(.+?)</name>");
							Matcher m = p.matcher(placeNameXML);
							
							if(m.find())
							{
								results.add(friendsMatcher.group(1) + " @ " + m.group(1));
							}
						}
					}
					
				}
			}
			
			String[] resultsArray = (String[])results.toArray();
			
			for(int i = 0; i < resultsArray.length; i++)
			{
				Log.d("Display the my Checkins that my Friends have also Checked into", resultsArray[i]);
			} 


		}
		catch (EasyFacebookError e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   */

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}


}
